﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderAdd.cs
    // 文件功能描述：新建采购单，采购单基本信息
    // 创建标识：吕海乐2009-05-12
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class PurchaseOrderAdd : BasePage
    {
        //public PurchaseOrderAdd()
        //{
        //    this.ModuleNo = Module.采购单管理.ToString();
        //    this.PageOperateType = OperateType.新增.ToString();
        //}

        IBLL_Purchase ip = Factory.BLL_Purchase();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定默认的采购单名称：所在机构名+时间+"采购单"
                txtPurchaseOrderName.Text = CurrentAccount.GetUser().Org.OrgName + DateTime.Now.ToString("yyyyMMddHHmm") + "采购单";
            }
        }

        /// <summary>
        /// 检查数据合法性的方法
        /// </summary>
        /// <returns></returns>
        private string DataCheck()
        {
            string errorMsg = "";
            //采购单名称为空
            if (string.IsNullOrEmpty(txtPurchaseOrderName.Text.Trim()))
            {
                errorMsg = "采购单名称不能为空！";
                txtPurchaseOrderName.Focus();
                return errorMsg;
            }

            //订单时间不能为空
            if (string.IsNullOrEmpty(txtPurchaseOrderTimeFrom.Text.Trim()))
            {
                errorMsg = "订单时间不能为空！";
                txtPurchaseOrderTimeFrom.Focus();
                return errorMsg;
            }
            //结束时间不能为空
            //if (string.IsNullOrEmpty(txtPurchaseOrderTimeTo.Text.Trim()))
            //{
            //    errorMsg = "结束时间不能为空！";
            //    txtPurchaseOrderTimeTo.Focus();
            //    return errorMsg;
            //}

            //订单开始时间不得早于现在的时间
            if (Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim()).Date < DateTime.Now.Date)
            {
                errorMsg = "采购开始时间不得晚于当前时间";
                txtPurchaseOrderTimeFrom.Focus();
                return errorMsg;
            }

            //订单开始时间晚于结束时间
            //if (Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim()) > Convert.ToDateTime(txtPurchaseOrderTimeTo.Text.Trim()))
            //{
            //    errorMsg = "采购开始时间晚于结束时间！";
            //    txtPurchaseOrderTimeFrom.Focus();
            //    return errorMsg;
            //}
            return errorMsg;

        }

        /// <summary>
        /// 保存采购单信息并跳转至添加采购单明细页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //检查数据
            if (!string.IsNullOrEmpty(DataCheck()))
            {
                string errorMsg = DataCheck();
                HZ.Web.WebUtil.Alert(errorMsg, this);
                return;
            }

            //添加采购单信息
            PurchaseOrder aPurchaseOrder = new PurchaseOrder();
            aPurchaseOrder.OrderID = Guid.NewGuid().ToString();
            aPurchaseOrder.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            aPurchaseOrder.OrderName = txtPurchaseOrderName.Text.Trim();
            aPurchaseOrder.StartTime = Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text.Trim());
            //aPurchaseOrder.PlanEndTime = Convert.ToDateTime(txtPurchaseOrderTimeTo.Text.Trim());
            aPurchaseOrder.PlanPerson = CurrentAccount.GetUser().UserID; 
            aPurchaseOrder.TotalAmount = 0;
            aPurchaseOrder.MODIFYTIME = DateTime.Now;
            aPurchaseOrder.MODIFYSTATUS = 0;
            aPurchaseOrder.UPLOADSTATUS = 0;
            if (!string.IsNullOrEmpty(txtRemarks.Text.Trim()))
            {
                aPurchaseOrder.Remarks = txtRemarks.Text;
            }
            else
            {
                aPurchaseOrder.Remarks = "";// "联系人：" + CurrentAccount.GetUser().UserName + "联系方式：无。";
            }

            int flag = ip.AddPurchaseOrder(aPurchaseOrder);

            if (flag == 1)
            {
                HZ.Web.WebUtil.RedirectByScript("PurchaseOrderDetailAdd.aspx?orderID=" + aPurchaseOrder.OrderID);
            }
            else if (flag == 2)
            {
                HZ.Web.WebUtil.Alert("采购单名称已经存在！", this);
                return;
            }
            else
            {
                HZ.Web.WebUtil.Alert("添加失败！", this);
            }

        }
    }
}
